BLOCK.Model="models/props_lab/hevplate.mdl"
BLOCK.BlockName = "Elevator";
BLOCK.Desc = "Transports people up and down";
BLOCK.HackTime = 5;
BLOCK.HackDuration = 15;

if(SERVER) then
	if(!ConVarExists( "num_elev" )) then
		CreateConVar("num_elev", 0, FCVAR_NONE);
	end
end

function BLOCK:Init()
	if(CLIENT) then
		self.start = Vector(0, 0, 0);
		self.last = Vector(0, 0, 0);
	end
	if(!SERVER) then return; end
	
	self.SENT.Entity:SetTrigger(true);
	self.SENT.Entity:SetCollisionGroup(COLLISION_GROUP_WORLD);
	
	local path_num = GetConVarNumber("num_elev");
	
	//Create path tracks
	self.path1 = ents.Create("path_track");
	self.path1:SetName("path" .. tostring(path_num));
	self.path1:SetAngles(Vector(90,0,0));
	self.path1:SetPos(self.SENT.Entity:GetPos());
	self.path1:SetKeyValue("target", "path" .. tostring(path_num+1));
	
	self.path2 = ents.Create("path_track");
	self.path2:SetAngles(Vector(90,0,0));
	self.path2:SetName("path" .. tostring(path_num+1));
	local p = self.SENT.Entity:GetPos();
	p.z = p.z + 150;
	self.path2:SetPos(p);
	self.path2:SetKeyValue("target", "path" .. tostring(path_num));
	
	self.path1:Spawn();
	self.path1:Activate();
	self.path2:Spawn();
	self.path2:Activate();
	
	//Create elevator entity
	self.elev = ents.Create("func_tracktrain");
	self.elev:SetPos(self.SENT.Entity:GetPos());
	self.elev:SetAngles(Vector(90,0,0));
	self.elev:SetKeyValue("target", "path" .. tostring(path_num));
	self.elev:SetKeyValue("speed", 25);
	self.elev:SetKeyValue("spawnflags", tostring(2+128));
	self.elev:Spawn();
	self.elev:Activate();
	
	self.elev:SetNotSolid(true);
	
	game.ConsoleCommand("num_elev " .. tostring(path_num+2) .. "\n");
	
	
	//Create a prop_dynamic
	self.prop = ents.Create("prop_dynamic");
	self.prop:SetModel("models/props_lab/blastdoor001b.mdl");
	self.prop:SetPos(self.SENT.Entity:GetPos());
	self.prop:SetAngles(Vector(90,0,0));
	self.prop:SetKeyValue("solid", "6");
	self.prop:Spawn();
	self.prop:Activate();
	self.prop:SetParent(self.elev);
	self.elev:Fire("StartForward");
	
	self.SENT.Entity:SetNetworkedFloat("elevHeight", 150);
	self.lastHeight = 150;
end

function BLOCK:Removed()
	self.elev:Remove();
	self.path1:Remove();
	self.path2:Remove();
	self.prop:Remove();
end

function BLOCK:Think()
	local curHeight = self.SENT.Entity:GetNetworkedFloat("elevHeight");
	if(curHeight != self.lastHeight) then
		local newpos = self.SENT.Entity:GetPos();
		newpos.z = newpos.z + curHeight;
		self.path2:SetPos(newpos);
		self.lastHeight = curHeight;
	end
end

function BLOCK:Hack( ply )
	self.elev:SetKeyValue("speed", 1000);
end

function BLOCK:EndHack()
	self.elev:SetKeyValue("speed", 25);
end

if(CLIENT) then 
	local Laser = Material( "cable/redlaser" );
	function BLOCK:Render()
		render.SetMaterial( Laser );
		local last = self.SENT.Entity:GetPos();
		last.z = last.z + self.SENT.Entity:GetNetworkedFloat("elevHeight");
		render.DrawBeam(self.SENT.Entity:GetPos(), last, 5, 0, 0, Color(255,255,255,255));
		return true;
	end
	
	function BLOCK:DrawCPanel( p )
		local slide = p:NumSlider("Height", nil, 150, 400, 0);
		local h = self.SENT.Entity:GetNetworkedFloat("elevHeight");
		slide:SetValue( h );
		slide:InvalidateLayout(true);
		local this = self;
		function slide:OnValueChanged(val) this:ConVar("elevHeight", val); end
		return true;
	end
end